package njupt.daoimp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.List;

import njupt.dao.OrderDAO;
import njupt.vo.Cart;

public class OrderDAOimp implements OrderDAO {
	private Connection conn=null;
	private PreparedStatement ps=null ;
	private ResultSet rs=null;
   public OrderDAOimp(Connection conn){
		
		this.conn=conn;
		System.out.println("123");
		}
   public int createorder(Cart cart, String userid) throws Exception{            //生产订单，就是购买时生产订单。
	      int orderidReturn=0;
		String sql="insert into ordermsg(orderid,ID) values(emp_sequence.nextval,?)";//ID表示userid
		String sql1="select max(orderid) from ordermsg";
		System.out.println("sql"+sql);
		ps=this.conn.prepareStatement(sql);
		int ID=Integer.parseInt(userid);
		ps.setInt(1, ID);
		System.out.println("sql"+sql);
		this.ps.executeUpdate();
		ps=this.conn.prepareStatement(sql1);
		rs=this.ps.executeQuery();
		if(rs.next()){
			 orderidReturn=rs.getInt(1);
		}
			return orderidReturn;
		
   }
public List<String[]> selectorder(int userid) throws Exception {                 //biaoshi 
	  List<String[]> ordermsg=new ArrayList<String[]>();
	try{
		String sql="select orderid,status,buytime,payway from ordermsg where ID=?";
		ps=this.conn.prepareStatement(sql);
		ps.setInt(1, userid);
		rs=this.ps.executeQuery();
		//ResultSetMetaData rsmd=rs.getMetaData();
		//int 
	    while(rs.next()){
	    	String[]  str = new String[4]; 
	    	for(int i=0;i<4;i++){
	    		str[i]=rs.getString(i+1);
	    	}
	    	ordermsg.add(str);
	    }
	    
	}catch(Exception e){
		throw e;
	}
	return ordermsg;
}

public int createorderdetail(Cart cart,int orderid) throws Exception {       //生产订单详情。
	int flag=0;
	try{
		String sql="insert into orderdetail values(?,?,?)";
		System.out.println("SQL"+sql);
		ps=this.conn.prepareStatement(sql);
		for(int i=0;i<cart.getIdlist().size();i++){		   
		   ps.setInt(1, orderid);
		   ps.setInt(2,Integer.parseInt(cart.getIdlist().get(i)));
		   ps.setInt(3, cart.getShulianglist().get(i));
		   System.out.println("addbatch  "+sql);
		   ps.addBatch();
		}
		this.ps.executeBatch();
		flag=1;
	}catch(Exception e){
		throw e;
	}
	return flag;
}
/* (non-Javadoc)
 * @see njupt.dao.OrderDAO#createordermsg(njupt.vo.Cart, java.lang.String)
 */
/* (non-Javadoc)
 * @see njupt.dao.OrderDAO#selectorderdetail(int, int)
 */
public List<String[]> selectorderdetail(int orderid)
		throws Exception {
	   List<String[]> orderdetail=new ArrayList<String[]>();
	   try{
		   
		    String sql="select orderdetail.orderid ,goodsinfo.goodsname, goodsinfo.price ,orderdetail.goodsid ,orderdetail.goodscount from goodsinfo,orderdetail where goodsinfo.goodsid in(select orderdetail.goodsid from orderdetail where orderdetail.orderid=?)and goodsinfo.goodsid = orderdetail.goodsid and orderdetail.orderid=?" ;
           /* String sql1="select count(*)FROM (select orderdetail.orderid ,goodsinfo.goodsname, goodsinfo.price ,orderdetail.goodsid ,orderdetail.goodscount from goodsinfo,orderdetail where (goodsinfo.goodsid in(select orderdetail.goodsid from orderdetail where orderid=?))and goodsinfo.goodsid = orderdetail.goodsid)";
            
            ps=this.conn.prepareStatement(sql1);                //计算行数
            ps.setInt(1, orderid);
            int hangshu=this.ps.executeQuery().getInt(1);              */
            
            ps=this.conn.prepareStatement(sql);                     //执行sql
            ps.setInt(1, orderid);
            ps.setInt(2, orderid);
            rs=this.ps.executeQuery();
      //      ResultSetMetaData rsmd=rs.getMetaData();
           
           while(rs.next()){
        	   String[] str=new String[5]; 
          
               for(int i=0;i<5;i++){   
            	str[i]=rs.getString(i+1);
            }
               orderdetail.add(str);
           }       
 	   }catch(Exception e){
 		   throw e;
 		   }
	return orderdetail;
}
/* Overriding methods
 */
public int updateBuyStatus(Cart cart,int orderid) throws Exception {
	int flag=0;
	try{
		   
	    String sql="update ordermsg set status='已付款' where orderid=? " ;       
        ps=this.conn.prepareStatement(sql);                     //执行sql
        ps.setInt(1, orderid);     
        this.ps.executeUpdate();    
         flag=1;      
	   }catch(Exception e){
		   throw e;
		   }
	return flag;
}

	


}
